package com.xiyun.admin.controller;

import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiyun.common.response.result.R;
import com.xiyun.admin.entity.Student;
import com.xiyun.admin.entity.vo.StudentQuery;
import com.xiyun.admin.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@Api(description = "学生管理")
@RestController
@RequestMapping("/xiyun/service/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 从数据库读取所有学生信息
     * @return  返回学生列表
     */
    @ApiOperation(value = "获取学生列表")
    @ResponseBody
    @GetMapping("getStudentList")
    public R getStudentList(){
        List<Student> studentList = studentService.list(null);
        return R.ok().data("students",studentList);
    }

    @ApiOperation(value = "添加学生")
    @PostMapping("")
    public R addTeacher(@RequestBody Student student){
        return studentService.save(student) ? R.ok() : R.error();
    }

    @ApiOperation(value = "删除对应id的学生信息")
    @DeleteMapping("remove/{id}")
    public R delStudent(
            @ApiParam(name = "id",value = "学生id",required = true)
            @PathVariable String id){
        boolean b = studentService.removeById(id);
        if (b)
            return R.ok();
        else
            return R.error();
    }

    @ApiOperation(value = "组合条件查询")
    @PostMapping("listStudentCondition/{current}/{size}")
    public R listStudentCondition(
            @ApiParam(name = "current",value = "当前页",required = true)
            @PathVariable long current,
            @ApiParam(name = "size",value = "单页行数",required = true)
            @PathVariable long size,
            StudentQuery query){
        //创建对象
        Page<Student> page = new Page<>(current,size);

        //条件查询
        QueryWrapper<Student> wrapper = new QueryWrapper<>();

        //获取信息
        String name = query.getName();
        Integer gender = query.getGender(); //0男 1女
        String begin = query.getBegin();
        String end = query.getEnd();

        if (!StringUtils.isEmpty(name)) {
            //构建条件
            wrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(gender)) {
            //构建条件
            wrapper.eq("gender",gender);
        }
        if (!StringUtils.isEmpty(begin)) {
            //构建条件
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)) {
            //构建条件
            wrapper.le("gmt_create",end);
        }
        //按时间排序
        wrapper.orderByDesc("gmt_create");

        studentService.page(page,wrapper);
        //总记录数
        long total = page.getTotal();
        //页面数据
        List<Student> records = page.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }

    /**
     * 通过id查找学生
     * @return  Student
     */
    @ApiOperation(value = "通过id查找学生信息")
    @GetMapping("getStudentById/{id}")
    public Student getStudentById(@PathVariable String id){
        return studentService.getById(id);
    }

    @ApiOperation(value = "修改学生信息")
    @PostMapping("modified")
    public R modifiedStudent(@RequestBody Student student){
        boolean b = studentService.updateById(student);
        if (b) {
            return R.ok();
        }
        return R.error();
    }
}
